package com.example.demo.controller.user;

import com.example.demo.entity.Result;
import com.example.demo.service.user.UserService;
import com.example.demo.utils.VerifyCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author: cj
 * @create: 17-12-21 17:11:18
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @Resource
    RedisTemplate redisTemplate;

    @Resource
    private VerifyCodeUtil verifyCode;
    private String verify;

    {
        verify = "verify";
    }


    /**
     * 账号登录
     *
     * @param name     账号
     * @param password 密码
     */
    @PostMapping("/accountLogin")
    public Result accountLogin(@RequestParam("name") String name, @RequestParam("password")String password) {
        return Result.ok( "登录成功", userService.accountLogin( name, password ) );
    }

    /**
     * 用户注册
     *
     * @param map 注册信息
     * @return 用户信息和token
     */
    @PostMapping("/accountRegister")
    public Result accountRegister(@RequestBody Map<String, Object> map) {
        return Result.ok( "注册成功", userService.accountRegister( map ) );
    }

    /**
     * 发送验证码，存储验证码
     */
    @GetMapping("/getYZm")
    public Result getYzm(String phone) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        if (!redisTemplate.hasKey( phone )) {
            int i = new Random().nextInt( 8999 ) + 1000;
            log.info( "验证码:" + i );
            opsForValue.set( phone, i + "", 5, TimeUnit.SECONDS );
            String name = opsForValue.get( phone );
            log.info( "验证码:" + name );
            return Result.ok( name );
        } else {
            throw new RuntimeException( "请勿重复发送验证码" );
        }
    }


    /**
     * 发送图片验证码
     * a、生成图片； b、保存图片上的文本到session域中； c、把图片响应给客户端。
     */
    @GetMapping("/getImgYzm")
    public Result getImgYzm(String phone, HttpServletResponse response) throws IOException {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String ver = verify + phone;
        // 生成图片
        BufferedImage bi = verifyCode.getImage();
        String text = verifyCode.getText();
        opsForValue.set( ver, text, 60 );
        VerifyCodeUtil.output( bi, response.getOutputStream() );
        return Result.ok( "ok" );
    }

    /**
     * 修改用户信息
     *
     * @param map 用户信息
     * @return 修改后的用户信息
     */
    @PostMapping("update")
    public Result update(@RequestBody Map<String, Object> map) {
        return Result.ok( userService.updateUser( map ) );
    }

    /**
     * 游客的唯一标识
     *
     * @param ip ip
     * @return 唯一标识
     */
    @GetMapping("/getId")
    private Result getId(String ip) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code;
        if (redisTemplate.hasKey( ip )) {
            Object o = valueOperations.get( ip );
            code = String.valueOf( o );
        } else {
            Map<String, Object> map = userService.getId( ip );
            code = map.get( "_id" ).toString();
            valueOperations.append( ip, code );
        }
        return Result.ok( code );
    }


}
